/* 封装网络请求方法 */
// 引入封装的request方法
const { request } = require('../utils/http')
const { returnBack } = require('../utils/util')
const app = getApp()

// 实体业务接口
module.exports = {
	//登录
	login: (tel, password, type) => {
		return request(
			'/login',
			'POST',
			{
				tel: tel,
				password, password,
				type: type
			}
		)
	},
	//注册
	register: (avatarUrl, nickname, tel, password, type, name) => {
		return request(
			'/register',
			'POST',
			{
				avatar: avatarUrl,
				nickname: nickname,
				tel: tel,
				password: password,
				type: type,
				name: name
			}
		)
	},
	//* 陪诊员注册
	workerRegister: (worker) => {
		return request(
			'/worker/register',
			'POST',
			worker
		)
	},

	//首页查询陪诊员列表
	getIndexWorkers: () => {
		return request(
			'/worker/indexWorkers',
			'GET'
		)
	},
	//首页查询所有用户订单
	getIndexOrders: () => {
		return request(
			'/order/allOrders',
			'GET'
		)
	},

	/** 服务流程 */
	//用户生成订单
	userOrder: (orderTypeId, tel, memberName, memberDetail, clinicTime, hospital, workerId) => {
		//如果没有指定陪诊员，就不传workerId，否则就传
		//要么传，要么不传，不会传了但为空，只要传了就一定有值
		if (workerId == '' || workerId == null) {
			return request(
				'/order/order',
				'POST',
				{
					orderTypeId: orderTypeId,
					memberId: app.globalData.currentUser.id,
					memberTel: tel,
					memberName: memberName,
					memberDetail: memberDetail,
					clinicTime: clinicTime,
					hospital: hospital
				}
			)
		} else {
			return request(
				'/order/order',
				'POST',
				{
					orderTypeId: orderTypeId,
					memberId: app.globalData.currentUser.id,
					memberTel: tel,
					memberName: memberName,
					memberDetail: memberDetail,
					clinicTime: clinicTime,
					hospital: hospital,
					workerId: workerId
				}
			)
		}
		
	},
	//支付
	pay: (orderId, money) => {
		return request(
			'/order/pay',
			'POST',
			{
				orderId: orderId,
				money: money
			}
		)
	},
	//接单
	receiveOrder: (orderId) => {
		return request(
			'/order/receiveOrder',
			'POST',
			{
				orderId: orderId,
				workerId: app.globalData.currentUser.id
			}
		)
	},
	//开始服务 & 确认完成
	changeOrder: (orderId, status) => {
		return request(
			'/order/startOrder',
			'POST',
			{
				orderId: orderId,
				status: status
			}
		)
	},
	//完成服务
	finishOrder: (orderId, situation, suggestion) => {
		return request(
			'/order/finishOrder',
			'POST',
			{
				orderId: orderId,
				situation: situation,
				suggestion: suggestion
			}
		)
	},
	//完成评价
	finishEvaluate: (orderId, eva, type) => {
		return request(
			'/order/finishEvaluate',
			'POST',
			{
				orderId: orderId,
				eva: eva,
				type: type
			}
		)
	},

	//根据id获取订单详情
	getOrder: (orderId) => {
		const { currentUser } = app.globalData
		//没登陆就跳转回首页
		if (currentUser == null || currentUser == '') {
			returnBack('/pages/hospital/users', '请先登录', 'error')
		} else {
			return request(
				'/order/getOrder',
				'GET',
				{
					orderId: orderId
				}
			)
		}
	},
	//获取用户订单列表
	getUserOrder: (orderStatus) => {
		//判断是用户还是陪诊员
		if (app.globalData.currentUser.type == '1') {
			return request(
				'/order/orderList',
				'GET',
				{
					memberId: app.globalData.currentUser.id,
					status: orderStatus
				}
			)
		} else if (app.globalData.currentUser.type == '2') {
			return request(
				'/order/workerOrderList',
				'GET',
				{
					workerId: app.globalData.currentUser.id,
					status: orderStatus
				}
			)
		}
		
	},
	//修改个人资料
	changeUserInfo: (currentUser) => {
		//先判断一下用户类型，是患者还是陪诊员
		if(currentUser.type == '1') {
			const member = currentUser
			return request(
				'/member/changeMemberInfo',
				'POST',
				member
			)
		} else if (currentUser.type == '2') {
			const worker = currentUser
			return request(
				'/worker/changeWorkerInfo',
				'POST',
				worker
			)
		}
	},
	//修改密码
	changeUserPassword: (password) => {
		return request(
			'/changePassword',
			'POST',
			{
				type: app.globalData.currentUser.type,
				id: app.globalData.currentUser.id,
				password: password
			}
		)
	},
	//用户添加就诊人信息
	addRelation: (relation) => {
		return request(
			'/relation/add',
			'POST',
			relation
		)
	},
	//查询当前用户所有就诊人
	getRelations: (memberId) => {
		return request(
			'/relation/getRelations',
			'GET',
			{
				memberId: memberId
			}
		)
	},
	//查询当前登录用户注册的陪诊员账号
	getIndexWorker: (memberId) => {
		return request(
			'/worker/selectIndexWorker',
			'GET',
			{
				memberId: memberId
			}
			
		)
	}

	
}